<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>日总结测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .test-section {
            margin: 20px 0;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        .success {
            color: #67c23a;
        }
        .error {
            color: #f56c6c;
        }
        .info {
            color: #909399;
        }
        button {
            margin: 5px;
            padding: 8px 16px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        .primary-btn {
            background-color: #409eff;
            color: white;
        }
        .success-btn {
            background-color: #67c23a;
            color: white;
        }
        .warning-btn {
            background-color: #e6a23c;
            color: white;
        }
        pre {
            background-color: #f5f7fa;
            padding: 10px;
            border-radius: 4px;
            overflow-x: auto;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>日总结功能测试</h1>
        
        <div class="test-section">
            <h3>测试日期格式匹配</h3>
            <p>当前日期格式测试：</p>
            <div id="dateTest"></div>
            <button class="primary-btn" onclick="testDateFormats()">测试日期格式</button>
        </div>

        <div class="test-section">
            <h3>模拟历史记录数据</h3>
            <p>创建模拟的历史记录数据：</p>
            <button class="success-btn" onclick="createMockHistory()">创建模拟数据</button>
            <button class="warning-btn" onclick="clearHistory()">清空历史记录</button>
            <div id="historyResult"></div>
        </div>

        <div class="test-section">
            <h3>测试日总结生成</h3>
            <p>测试日总结功能：</p>
            <button class="primary-btn" onclick="testDailySummary()">测试日总结</button>
            <div id="summaryResult"></div>
        </div>

        <div class="test-section">
            <h3>本地存储检查</h3>
            <p>检查本地存储中的数据：</p>
            <button class="primary-btn" onclick="checkLocalStorage()">检查本地存储</button>
            <div id="storageResult"></div>
        </div>
    </div>

    <script>
        // 测试日期格式
        function testDateFormats() {
            const now = new Date();
            const date1 = now.toLocaleDateString();
            const date2 = now.toLocaleDateString('zh-CN');
            
            const result = `
                <p class="info">toLocaleDateString(): <span class="${date1 === date2 ? 'success' : 'error'}">${date1}</span></p>
                <p class="info">toLocaleDateString('zh-CN'): <span class="${date1 === date2 ? 'success' : 'error'}">${date2}</span></p>
                <p class="${date1 === date2 ? 'success' : 'error'}">
                    ${date1 === date2 ? '✓ 日期格式匹配' : '✗ 日期格式不匹配'}
                </p>
            `;
            
            document.getElementById('dateTest').innerHTML = result;
        }

        // 创建模拟历史记录
        function createMockHistory() {
            const today = new Date().toLocaleDateString('zh-CN');
            const mockRecord = {
                date: today,
                time: '14:30:00',
                timestamp: Date.now(),
                total: 8,
                present: 6,
                leave: 1,
                absent: 1,
                late: 0,
                delay: 0,
                checker: '测试用户',
                type: 'dormitory',
                students: [
                    { studentId: '2021001', name: '张三', dormitory: '101', status: 'present' },
                    { studentId: '2021002', name: '李四', dormitory: '102', status: 'present' },
                    { studentId: '2021003', name: '王五', dormitory: '103', status: 'leave' },
                    { studentId: '2021004', name: '赵六', dormitory: '101', status: 'absent' },
                    { studentId: '2021005', name: '钱七', dormitory: '102', status: 'present' },
                    { studentId: '2021006', name: '孙八', dormitory: '104', status: 'present' },
                    { studentId: '2021007', name: '周九', dormitory: '105', status: 'present' },
                    { studentId: '2021008', name: '吴十', dormitory: '103', status: 'present' }
                ]
            };

            // 保存到本地存储
            const dormitoryHistory = JSON.parse(localStorage.getItem('dormitoryHistory') || '[]');
            dormitoryHistory.unshift(mockRecord);
            localStorage.setItem('dormitoryHistory', JSON.stringify(dormitoryHistory));

            document.getElementById('historyResult').innerHTML = `
                <p class="success">✓ 模拟历史记录已创建</p>
                <pre>${JSON.stringify(mockRecord, null, 2)}</pre>
            `;
        }

        // 清空历史记录
        function clearHistory() {
            localStorage.removeItem('dormitoryHistory');
            localStorage.removeItem('classHistory');
            document.getElementById('historyResult').innerHTML = '<p class="success">✓ 历史记录已清空</p>';
        }

        // 测试日总结
        function testDailySummary() {
            const dormitoryHistory = JSON.parse(localStorage.getItem('dormitoryHistory') || '[]');
            const today = new Date().toLocaleDateString('zh-CN');
            
            const todayRecords = dormitoryHistory.filter(record => record.date === today);
            
            if (todayRecords.length === 0) {
                document.getElementById('summaryResult').innerHTML = `
                    <p class="error">✗ 今天没有查寝记录</p>
                    <p class="info">当前日期: ${today}</p>
                    <p class="info">历史记录中的日期:</p>
                    <ul>
                        ${dormitoryHistory.map(record => `<li>${record.date}</li>`).join('')}
                    </ul>
                `;
            } else {
                const latestRecord = todayRecords[0];
                const summaryData = {
                    type: 'daily',
                    date: today,
                    total: latestRecord.total,
                    present: latestRecord.present,
                    absent: latestRecord.absent,
                    leave: latestRecord.leave,
                    late: latestRecord.late,
                    delay: latestRecord.delay || 0,
                    leaveStudents: latestRecord.students.filter(s => s.status === 'leave')
                };

                document.getElementById('summaryResult').innerHTML = `
                    <p class="success">✓ 日总结生成成功</p>
                    <pre>${JSON.stringify(summaryData, null, 2)}</pre>
                `;
            }
        }

        // 检查本地存储
        function checkLocalStorage() {
            const dormitoryHistory = localStorage.getItem('dormitoryHistory');
            const classHistory = localStorage.getItem('classHistory');
            
            let result = '<h4>宿舍历史记录:</h4>';
            if (dormitoryHistory) {
                const records = JSON.parse(dormitoryHistory);
                result += `<p class="info">记录数量: ${records.length}</p>`;
                result += '<ul>';
                records.slice(0, 3).forEach(record => {
                    result += `<li>${record.date} - ${record.time} (${record.present}/${record.total})</li>`;
                });
                result += '</ul>';
            } else {
                result += '<p class="error">没有宿舍历史记录</p>';
            }

            result += '<h4>班级历史记录:</h4>';
            if (classHistory) {
                const records = JSON.parse(classHistory);
                result += `<p class="info">记录数量: ${records.length}</p>`;
                result += '<ul>';
                records.slice(0, 3).forEach(record => {
                    result += `<li>${record.date} - ${record.time} (${record.present}/${record.total})</li>`;
                });
                result += '</ul>';
            } else {
                result += '<p class="error">没有班级历史记录</p>';
            }

            document.getElementById('storageResult').innerHTML = result;
        }

        // 页面加载时执行初始测试
        window.onload = function() {
            testDateFormats();
            checkLocalStorage();
        };
    </script>
</body>
</html>